package prassein.rsm.web;

import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import prassein.rsm.common.utils.Constants;
import prassein.rsm.entity.base.Resume;
import prassein.rsm.service.base.AuditManager;
import prassein.rsm.service.base.ResumeManager;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Results({
	@Result(name = "success",location="reloadReviewResume.jsp"), 
	@Result(name = "error",location="login.jsp"),
	@Result(name = "input",location="resumeInsert.jsp")})
public class ResumeInsertAction extends ActionSupport {
	
	private static final long serialVersionUID = -793118724462377480L;
	
	@Autowired
	private ResumeManager resumeManager;
	
	@Autowired
	private AuditManager auditManager;
	
	private Resume resume = new Resume();
	
	private int resumeId = -1;
	
	private int submitResult = -1;
	
	@Override 
	public String execute() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get(Constants.USERNAME) != null && !session.get(Constants.USERNAME).equals("")) {
			try {
//				编辑resume, reload resume的逻辑
				if (resumeId == -1) {
					if (resume.getName() == null || resume.getName().equals("")
//							|| resume.getAddress() == null || resume.getAddress().equals("")
							|| resume.getContactNumber() == null || resume.getContactNumber().equals("")
							|| resume.getCorporation() == null || resume.getCorporation().equals("")
							|| resume.getDateOfBirth() == null || resume.getDateOfBirth().equals("")
							|| resume.getNationality() == null || resume.getNationality().equals("")
							|| resume.getPosition() == null || resume.getPosition().equals("")
							|| resume.getSex() == null || resume.getSex().equals("")
							|| resume.getWorkingPlace() == null || resume.getWorkingPlace().equals("")
							|| resume.getTypeOfWork() == null || resume.getTypeOfWork().equals("")) {
						session.remove(Constants.LATEST_EDIT_REUMEID);
						return INPUT;
					} else {
						if (resume.getContactNumber() != null) {
							resume.setContactNumber(resume.getContactNumber().replace("-", "").replace("(", "").replace(")", "").replace(" ", ""));
						}
						
						if (resume.getId() != null) {
							resumeManager.save(resume);
							session.put(Constants.LATEST_EDIT_REUMEID, resume.getId());
							submitResult = 1;
							resume = resumeManager.getResume(Integer.parseInt(session.get(Constants.LATEST_EDIT_REUMEID).toString()));
							
							auditManager.save(session.get(Constants.USERNAME).toString(), resume.getName(), "更新");
							return SUCCESS;
						} else {
							List<Resume> resumeList = resumeManager.queryResult(resume.getName(), resume.getContactNumber());
							if (resumeList.size() > 0) {
								submitResult = 3;
								return INPUT;
							} else {
								resumeManager.save(resume);
								session.put(Constants.LATEST_EDIT_REUMEID, resume.getId());
								submitResult = 1;
								resume = resumeManager.getResume(Integer.parseInt(session.get(Constants.LATEST_EDIT_REUMEID).toString()));
								auditManager.save(session.get(Constants.USERNAME).toString(), resume.getName(), "新建");
								return SUCCESS;
							}
						}
					}
				} else {
					session.put(Constants.LATEST_EDIT_REUMEID, resumeId);
					return SUCCESS;
				}
				
			} catch (Exception e) {
				submitResult = 2;
				return INPUT;
			}
		} else {
			return ERROR;
		}
	}
	
	public ResumeManager getResumeManager() {
		return resumeManager;
	}

	public void setResumeManager(ResumeManager resumeManager) {
		this.resumeManager = resumeManager;
	}

	public Resume getResume() {
		return resume;
	}

	public void setResume(Resume resume) {
		this.resume = resume;
	}

	public int getResumeId() {
		return resumeId;
	}

	public void setResumeId(int resumeId) {
		this.resumeId = resumeId;
	}

	public int getSubmitResult() {
		return submitResult;
	}

	public void setSubmitResult(int submitResult) {
		this.submitResult = submitResult;
	}

	public AuditManager getAuditManager() {
		return auditManager;
	}

	public void setAuditManager(AuditManager auditManager) {
		this.auditManager = auditManager;
	}
}
